# """ 验收台操作
# @Project: Rebar
# @File: station_mapper.py
# @Author: hzc
# @Date: 2023/3/29 19:25
# """
# import datetime
# from contextlib import ExitStack
# from typing import Union
#
# from sqlalchemy.exc import IntegrityError
#
# from config import SUPER_ADMIN
# from model.db import rebar, TbInspectionStation, TbCompany, TbProject
# from model.db.mapper.company_mapper import get_company_by_name, add_company
# from model.db.mapper.project_mapper import get_project_by_name, update_project, add_project
# from model.params import UpdateUserInfoParam, StationInfoParam, CompanyInfoParam
# from utils import DataNotExist, AllException
#
#
# def get_station(station_id) -> TbInspectionStation:
#     """
#     根据station_id获取验收台信息
#     :param station_id:
#     :return:
#     """
#     station = rebar.query(TbInspectionStation).filter_by(station_id=station_id).first()
#     if not station:
#         raise DataNotExist("验收台")
#     return station
#
#
# def add_station(station_info: StationInfoParam) -> TbInspectionStation:
#     """
#     添加验收台
#     :param station_info:
#     :return:
#     """
#     company, project = None, None
#     rebar.begin_nested()
#     try:
#         try:  # 检查公司是否存在，若不存在，创建公司
#             company = get_company_by_name(station_info.company_name)
#             rebar.delete(company)
#             rebar.commit()
#             project = get_project_by_name(station_info.project_name)
#             rebar.delete(project)
#             rebar.commit()
#         except DataNotExist:
#             pass
#         # 创建公司
#         company_info = CompanyInfoParam(
#             company_name=station_info.company_name
#         )
#         company = TbCompany(
#             company_name=company_info.company_name,
#             company_corporate=company_info.company_corporate,
#             corporate_phone=company_info.corporate_phone,
#             remark=company_info.remark
#         )
#         rebar.add(company)
#         rebar.commit()
#         # 创建项目
#         project = TbProject(
#             project_name=station_info.project_name,
#             project_address=station_info.project_address,
#             project_start_time=station_info.project_start_time,
#             project_end_time=station_info.project_end_time,
#             company_id=company.company_id,
#             remark=station_info.remark
#         )
#         rebar.add(project)
#         rebar.commit()
#
#         # 添加验收台
#         station = TbInspectionStation(
#             project_id=project.project_id,
#             station_name=station_info.station_name,
#             station_contacts=station_info.station_contacts,
#             contacts_phone=station_info.contacts_phone,
#             station_start_time=station_info.station_start_time,
#             remark=station_info.remark
#         )
#         rebar.add(station)
#         rebar.commit()
#         return station
#     except IntegrityError as e:
#         print("station_mapper.py", e, e.__traceback__.tb_lineno, sep="\n")
#         rebar.rollback()
#         raise e
#
#
# def update_station(station_id, station_info: UpdateUserInfoParam) -> TbInspectionStation:
#     """
#     更新验收台信息
#     :param station_id:
#     :param station_info:
#     :return:
#     """
#     pass
#
#
# def delete_station(station_id) -> bool:
#     """
#     删除验收台
#     :param station_id:
#     :return:
#     """
#     station = station_is_exist(station_id)
#     try:
#         rebar.delete(station)
#         rebar.commit()
#         return True
#     except Exception as e:
#         print("user_params.py", e, e.__traceback__.tb_lineno)
#         rebar.rollback()
#         raise AllException("删除验收台失败")
#
#
# def get_station_by_name(station_name) -> TbInspectionStation:
#     station = rebar.query(TbInspectionStation).filter_by(username=station_name).first()
#     if not station:
#         raise DataNotExist("验收台")
#     return station
#
#
# def get_stations_by_role_id(role_id) -> TbInspectionStation:
#     stations = rebar.query(TbInspectionStation).filter_by(role_id=role_id).all()
#     return stations
#
#
# def get_stations_by_project_id(project_id) -> TbInspectionStation:
#     stations = rebar.query(TbInspectionStation).filter_by(project_id=project_id).all()
#     return stations
#
#
# def activate_station(station_id) -> Union[TbInspectionStation, bool]:
#     """
#     激活验收台
#     :param station_id:
#     :return:
#     """
#     station = station_is_exist(station_id)
#     if station.is_authorize:  # 若验收台已经激活，不用下面操作了
#         return True
#     try:
#         station.is_authorize = 1
#         station.authorize_time = datetime.datetime.now()
#         rebar.commit()
#         return station
#     except Exception as e:
#         print("user_params.py", e, e.__traceback__.tb_lineno)
#         rebar.rollback()
#         raise AllException("验收台激活失败")
#
#
# def update_station_role(station_id, role_name) -> Union[TbInspectionStation, bool]:
#     """
#     修改验收台所属role
#     :param station_id:
#     :param role_name:
#     :return:
#     """
#     station = station_is_exist(station_id)
#     if station.username == SUPER_ADMIN:
#         raise AllException("管理员角色无法修改")
#     if station.role_name == role_name:  # 若角色名和之前的相同，无需修改
#         return True
#     try:
#         station.role_name = role_name
#         rebar.commit()
#         return station
#     except Exception as e:
#         print("user_params.py", e, e.__traceback__.tb_lineno)
#         rebar.rollback()
#         raise AllException("验收台角色修改失败")
#
#
# def update_station_company(station_id, company_id) -> Union[TbInspectionStation, bool]:
#     """
#     修改验收台所属company
#     :param station_id:
#     :param company_id:
#     :return:
#     """
#     station = station_is_exist(station_id)
#     if station.username == SUPER_ADMIN:
#         raise AllException("管理员隶属公司无法修改")
#     get_company(company_id)  # 判断公司是否存在
#     if station.company_id == company_id:  # 若角色名和之前的相同，无需修改
#         return True
#     try:
#         station.company_id = company_id
#         rebar.commit()
#         return station
#     except Exception as e:
#         print("user_params.py", e, e.__traceback__.tb_lineno)
#         rebar.rollback()
#         raise AllException("验收台隶属公司修改失败")
#
#
# def station_is_exist(station_id) -> TbInspectionStation:
#     """
#     判断验收台是否存在，若存在，则返回user
#     :param station_id:
#     :return:
#     """
#     station = get_station(station_id)
#     if not station:
#         raise DataNotExist("验收台")
#     return station
